<?php
function view_data(){
	$crud = $_REQUEST['crud'];

	switch ($crud){
		case "input": input_data_view();break;
		case "hapus": hapus_data_view();break;		
		default: list_data_view();break;
	}
}

	
/*-------------------------------------------------
----------------------Input------------------------
-------------------------------------------------*/	
function input_data_view(){	
global $wpdb;
$no=$_GET['no'];
	?>
    <div class="wrap">
    <div id="icon-users" class="icon32"></div>
    </div>
    <h2>Pendaftaran Data Penelitian</h2>
	<?
    if ( isset( $_POST['nama_penelitian'] ) ) {
        $nama_penelitian  =  $_POST['nama_penelitian'];
        $tahun_penelitian =  $_POST['tahun_penelitian'];
        $dana			  =  $_POST['dana'];
        $judul_penelitian	  =  $_POST['judul_penelitian'];
        $keterangan	  =  $_POST['keterangan'];
		
		if($no==""){
			$wpdb->query("INSERT INTO `data_penelitian` VALUES ('','$nama_penelitian', '$tahun_penelitian', '$dana', '$judul_penelitian', '$keterangan')");
			echo "Data Berhasil di Simpan..";
		}else{
			$SQL="UPDATE `data_penelitian` SET `nama_penelitian`='$nama_penelitian', `tahun_penelitian`='$tahun_penelitian',
											   `dana`='$dana', `judul_penelitian`='$judul_penelitian', `keterangan`='$keterangan' WHERE `no`='$no'";
		$wpdb->query($SQL);
			echo "Data Berhasil di Ubah..";
		}											   
    }
	
	if($no != ""){
		$datacari = $wpdb->get_results("SELECT * FROM data_penelitian WHERE no = '$no'");
		foreach($datacari as $row){
	?>
	<form id="form1" name="form1" method="post" action="" class="validate">
		<table width='100%' border='0' cellspacing='2' cellpadding='2' class='form-table'>
		
		<tr class="form-field form-required">
		  <th><label for="nama_penelitian">Nama Penelitian <span class="description">(required)</span></label></th>
		  <td>
			<input type="text" name="nama_penelitian" id="nama_penelitian" value="<?php echo $row->nama_penelitian;?>" class="regular-text" maxlength="25">
			<br />
		  </td>
		</tr>

		<tr>
		  <th><label for="judul_penelitian">Judul Penelitian</label></th>
		  <td>
			<textarea  id="judul_penelitian" name="judul_penelitian" rows="5" cols="80"><?php echo $row->judul_penelitian;?></textarea>
			<br />
		  </td>
		</tr>

		<tr>
		  <th class="form-field form-required"><label for="tahun_penelitian">Tahun Penelitian <span class="description">(required)</span></label></th>
		  <td>
			<input type="text" name="tahun_penelitian" id="tahun_penelitian" value="<?php echo $row->tahun_penelitian;?>" class="regular-text" maxlength="15">
			<br />
		  </td>
		</tr>

		<tr>
		  <th><label for="dana">Dana</label></th>
		  <td>
			<input type="text" id="dana" name="dana" value="<?php echo $row->dana;?>" class="regular-text" maxlength="15">
			<br />
		  </td>
		</tr>
        
		<tr>
		  <th><label for="keterangan">Keterangan</label></th>
		  <td>
			<input type="text" id="keterangan" name="keterangan" value="<?php echo $row->keterangan;?>" class="regular-text">
			<br />
		  </td>
		</tr>
	  </table>
          <br>
	<b><input type='submit' name='submit' value=' Daftar '></b>&emsp;<b><input type='reset' name='reset' value=' Batal '></b>
</form>
<? }
}else{ ?>
	<form id="form1" name="form1" method="post" action="">
		<table width='100%' border='0' cellspacing='2' cellpadding='2' class='form-table'>
		
		<tr class="form-field form-required">
		  <th><label for="nama_penelitian">Nama Penelitian <span class="description">(required)</span></label></th>
		  <td>
			<input type="text" name="nama_penelitian" id="nama_penelitian" value="<?php echo $row->nama_penelitian;?>" class="regular-text" maxlength="15">
			<br />
		  </td>
		</tr>

		<tr>
		  <th><label for="judul_penelitian">Judul Penelitian</label></th>
		  <td>
			<textarea  id="judul_penelitian" name="judul_penelitian" rows="5" cols="80"><?php echo $row->judul_penelitian;?></textarea>
			<br />
		  </td>
		</tr>

		<tr>
		  <th class="form-field form-required"><label for="tahun_penelitian">Tahun Penelitian <span class="description">(required)</span></label></th>
		  <td>
			<input type="text" id="tahun_penelitian" name="tahun_penelitian" value="<?php echo $row->tahun_penelitian;?>" class="" maxlength="4" size="5">
			<br />
		  </td>
		</tr>

		<tr>
		  <th><label for="dana">Dana</label></th>
		  <td>
			<input type="text" id="dana" name="dana" value="<?php echo $row->dana;?>" class="regular-text" maxlength="15">
			<br />
		  </td>
		</tr>
        
		<tr>
		  <th><label for="keterangan">Keterangan</label></th>
		  <td>
			<input type="text" id="keterangan" name="keterangan" value="<?php echo $row->keterangan;?>" class="regular-text">
			<br />
		  </td>
		</tr>
	  </table>
          <br>
	<b><input type='submit' name='submit' value=' Daftar '></b>&emsp;<b><input type='reset' name='reset' value=' Batal '></b>
</form>
<?php } ?>
<h3><a href="admin.php?page=<?=$_REQUEST['page']?>&crud=view">Kembali</a></h3>
<?
}

/*-------------------------------------------------
----------------------List-------------------------
-------------------------------------------------*/	
function list_data_view(){	
	$data_cari = $_POST['data_cari'];
	?>
    <div class="wrap">
    <div id="icon-users" class="icon32"></div>
    </div>
    <h2>Perubahan Data Penelitian</h2>
		
    <form id="form1" name="form1" method="post" action="">
 	<input type="text" id="data_cari" name="data_cari" value="<?php echo $data_cari;?>" class="regular-text">
			<span class="description">Masukkan kata yang akan dicari !! &nbsp;<input type="submit" value=" Search !! " /></span>
          
		<table width="100%" cellpadding="2" cellspacing="2" border="1" class="form-table">
        <thead>
        	<tr>
        		<th align="center" width="2%" bgcolor="#666" style="color:#FFFFFF"><strong>No</strong></th>
        		<th align="center" width="60%" bgcolor="#666" style="color:#FFFFFF"><strong>Nama Penelitian</strong></th>
				<th align="center" width="60%" bgcolor="#666" style="color:#FFFFFF"><strong>Judul Penelitian</strong></th>
        		<th align="center" width="30%" bgcolor="#666" style="color:#FFFFFF"><strong>Tahun Penelitian</strong></th>
				<th align="center" width="30%" bgcolor="#666" style="color:#FFFFFF"><strong>Dana Penelitian</strong></th>
				<th align="center" width="30%" bgcolor="#666" style="color:#FFFFFF"><strong>Keterangan</strong></th>
        		<th align="center" width="8%" bgcolor="#666" style="color:#FFFFFF"><strong>Aksi</strong></th>
				
			</tr>
       </thead>     
	<?php 
	global $wpdb;
	$no=1;
	$hasilcari = Array(); 
	$hasilcari = $wpdb->get_results("SELECT * FROM data_penelitian WHERE nama_penelitian  LIKE '%$data_cari%' || tahun_penelitian LIKE '%$data_cari%'");
	foreach($hasilcari as $data){ ?>
		<tr>
        <td width="2%"><?php echo $no++; ?></td>
		<td width="60%"><?php echo  $data->nama_penelitian; ?></td>
		<td width="30%"><?php echo  $data->judul_penelitian; ?></td>
		<td width="30%"><?php echo  $data->tahun_penelitian; ?></td>
		<td width="30%"><?php echo  $data->dana; ?></td>
		<td width="30%"><?php echo  $data->keterangan; ?></td>
		<td width="8%">
		<a href="admin.php?page=<?php echo $_REQUEST['page']."&crud=input&no=".$data->no; ?>">Edit</a>
		<a href="admin.php?page=<?php echo $_REQUEST['page']."&crud=hapus&no=".$data->no; ?>">Hapus</a>
		</td></tr>
		<?php } ?>
		</table><br />
		<a href="admin.php?page=<?php echo $_REQUEST['page']; ?>&crud=input"><strong><h3>Tambah Data Penelitian</h3></strong></a>
	</form>
	<?php } 


/*-------------------------------------------------
---------------------Hapus-------------------------
-------------------------------------------------*/	
function hapus_data_view(){	    
global $wpdb; ?>
<center><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<style>
.fokus{border-collapse: collapse; margin-top: 0.5em; margin-bottom: -8px; clear: both; color: rgb(51, 51, 51); font-family: sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 16.78333282470703px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);  }
.y{ padding: 10px; width:40px;  background: #fff;  -webkit-box-shadow: inset 0px 0px 10px rgba(0,0,0,0.20); -moz-box-shadow: inset 0px 0px 10px rgba(0,0,0,0.20);
    box-shadow: inset 0px 0px 10px rgba(0,0,0,0.20); float:left; margin-left: 30%; cursor:pointer;}
.t{ padding: 10px; width:44px;  background: #fff;  -webkit-box-shadow: inset 0px 0px 10px rgba(0,0,0,0.20); -moz-box-shadow: inset 0px 0px 10px rgba(0,0,0,0.20);
    box-shadow: inset 0px 0px 10px rgba(0,0,0,0.20); float:right; margin-right: 30%; cursor:pointer;}

</style>
<?php 
if($_GET['aksi']=="true"){
$cc=$_GET['no'];
$wpdb->query("DELETE from data_penelitian where no='$cc';");
echo "<br><br><b>Data Berhasil di hapus.<br>"; ?>
<input type="submit" value="kembali" onClick="location.href='admin.php?page=<?php echo $_REQUEST['page']; ?>&crud=view'">
<?php
}else{
?>
<form method="get" action="" enctype="multipart/form-data">
<input type="hidden" name="dell" value="<?php echo $cc=$_GET['no'];?>" />
<table width="400px" border="1" cellspacing="2" cellpadding="2" class="fokus">
  <tr>
    <td height="33" bgcolor="#999">&emsp; <strong>Massage !!</strong></td>
  </tr>
  <tr>
    <td align="center"> <br />
       Apakah anda yakin ?? <br /><br />
       <div class="y" onClick="location.href='admin.php?page=<?php echo $_REQUEST['page']; ?>&crud=hapus&no=<?php echo $cc; ?>&aksi=true'">Hapus</div>
       <div class="t" onClick="history.go(-1)">Kembali</div><br />
    </td>
  </tr>
</table>
</form>
<?php } ?>
</div></center>

<?php } ?>